//
//  B_MyMessageViewController.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/19.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_MyMessageViewController.h"
#import "B_MessageListTVC.h"
#import "B_InteractMessageVC.h"
#import "B_SystemMessageVC.h"
#import "B_SystemListMessageModel.h"

@interface B_MyMessageViewController ()

@property (nonatomic, strong) BaseTableView * mainView;

@end

@implementation B_MyMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"消息"];
    self.view = self.mainView;
    [self getData:YES];
}

- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_MessageListTVC" bundle:nil] forCellReuseIdentifier:@"B_MessageListTVC"];
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.dataArray.count + 2;
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 80;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_MessageListTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_MessageListTVC" forIndexPath:indexPath];
            cell.B_imageView.backgroundColor = [UIColor redColor];
            cell.B_timeLabel.text = @"03-23";
            if (indexPath.row == 0) {
                cell.B_titleLabel.text = @"系统消息";
                cell.B_contentLabel.text = @"您有一条新的消息";
            } else if (indexPath.row == 1) {
                cell.B_titleLabel.text = @"互动消息";
                cell.B_contentLabel.text = @"hello 小姐姐在不在?";
            } else {
                cell.B_titleLabel.text = @"啊哈哈哈哈";
                cell.B_contentLabel.text = @"hello 小姐姐在不在?";
            }
            return cell;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.row == 0) {           // 系统消息
                B_SystemMessageVC * system = [[B_SystemMessageVC alloc] init];
                [weSelf pushNextViewController:system];
            } else if (indexPath.row == 1) {    // 互动消息
                B_InteractMessageVC * interact = [[B_InteractMessageVC alloc] init];
                [weSelf pushNextViewController:interact];
            } else {
                
            }
        };
        
    }
    return _mainView;
}


- (void) getData:(BOOL) isRefresh {
    if (isRefresh) {
        self.index = 1;
    } else {
        self.index ++;
    }
    WeakSelf(weSelf, self);
    NSString * urlString = [NSString stringWithFormat:@"/yearnfor/inter/notice/selectNoticeList/%@/%ld/10", getUserId(), (long)self.index];
    [NetworkingManager getNetworkRequestWithUrlString:urlString parameters:nil isCache:isRefresh succeed:^(id data) {
        [weSelf.mainView endRefresh];
        if ([data[@"code"] isEqualToString:@"20010"]) {
            if (isRefresh) {
                [self.dataArray removeAllObjects];
            }
            for (NSDictionary * dictionary in data[@"values"]) {
                B_SystemListMessageModel * model = [B_SystemListMessageModel mj_objectWithKeyValues:dictionary];
                [self.dataArray addObject:model];
            }
            [weSelf.mainView.tableView reloadData];
           
        }
    } fail:^(NSError *error) {
        [weSelf.mainView endRefresh];
        [KeyWindow makeToast:@"服务器异常,请售后重试!"];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
